package marcis.jarina.directory.repository;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JarVersion implements Comparable<JarVersion> {

    /**
     * numeric part of version
     */
    private String numericPart;

    /**
     * version including SNAPSHOT if applicable
     */
    private String fullVersion;

    private double value;

    private JarVersion() {

    }

    public JarVersion(String version) {
        JarVersion jarVersion = processVersion(version);
        this.setFullVersion(jarVersion.getFullVersion());
        this.setNumericPart(jarVersion.getNumericPart() == null ? "0" : jarVersion.getNumericPart());
        this.setValue(jarVersion.getValue());
    }

    public static JarVersion valueOf(String str) {
        return processVersion(str);
    }

    public JarVersion(File file) {
        JarVersion jarVersion = processVersion(stripSuffix(file));
        if (jarVersion != null) {
            this.setFullVersion(jarVersion.getFullVersion());
            this.setNumericPart(jarVersion.getNumericPart());
            this.setValue(jarVersion.getValue());
        }
    }

    private String stripSuffix(File file) {
        String name = file.getName();
        return name.substring(0, name.lastIndexOf("."));
    }


    private static JarVersion processVersion(String fullVersion) {
        if (fullVersion == null || fullVersion.length() == 0) {
            return null;
        }
        // 1.2.30-SNAPSHOT
        Pattern pat = Pattern.compile("(\\d+\\.)+\\d+");
        Matcher mat = pat.matcher(fullVersion);
        String numericPart = null;
        if (mat.find()) {
            // 12.3.45
            numericPart = mat.group();
        } else {
            // can not retrieve version information - set default 0.0
            JarVersion emptyVersion = new JarVersion();
            emptyVersion.setFullVersion(fullVersion);
            emptyVersion.setValue(0.0);
            emptyVersion.setNumericPart("0.0");
            return emptyVersion;
        }
        String[] exploded = numericPart.split("\\.");
        // 12.34.56 = 12.3456 ?
        double exponent = 1.0;
        double value = 0.0;
        for (String cislo : exploded) {
            value += exponent * Double.valueOf(cislo);
            exponent /= 100.0;
        }
        JarVersion jarVersion = new JarVersion();
        jarVersion.setFullVersion(fullVersion);
        jarVersion.setNumericPart(numericPart);
        jarVersion.setValue(value);
        return jarVersion;
    }

    public String getNumericPart() {
        return numericPart;
    }

    public String getFullVersion() {
        return fullVersion;
    }

    public double getValue() {
        return value;
    }

    @Override
    public String toString() {
        return "JarVersion{" +
                "fullVersion='" + fullVersion + '\'' +
                '}';
    }

    @Override
    public int compareTo(JarVersion o) {
        final double delta = 1e-6;
        if (Math.abs(this.getValue() - o.getValue()) < delta) {
            return 0;
        }
        return this.getValue() < o.getValue() ? -1 : 1;
    }

    void setNumericPart(String numericPart) {
        this.numericPart = numericPart;
    }

    void setFullVersion(String fullVersion) {
        this.fullVersion = fullVersion;
    }

    void setValue(double value) {
        this.value = value;
    }

    public static void main(String[] a) {
        JarVersion app = new JarVersion("3.0.0.RELEASE");
        System.out.print(app.getNumericPart());
    }
}
